package cool.xinya.ess.util;

import cool.xinya.ess.common.Result;
import cool.xinya.ess.common.ResultEnum;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

/**
 * 生成Result对象的工具类，可根据实际业务情况进行扩展
 *
 * @author jiangwenbo
 * @date 2019/6/17.
 */
public class ResultUtil {
    public static Result error(Integer code, String msg) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        result.setData(null);
        return result;
    }

    public static Result success(Integer code, String msg, Object obj) {
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        result.setData(obj);
        return result;
    }

    public static Result error(String msg) {
        return error(ResultEnum.UNKNOWN_ERROR.getCode(), msg);
    }

    public static Result success() {
        return success(null);
    }

    public static Result success(Object obj) {
        Map<String, Object> map = new HashMap<>();
        if (obj instanceof Collection) {
            map.put("list", obj);
            obj = map;
        }
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMsg());
        result.setData(obj);
        return result;
    }
}
